﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Axe.Extensions;
using System.Web.UI;

namespace Axe.Web.Controls
{
    public class ClickOnceButton : Button
    {
        [DefaultValue(true), Category("Behavior")]
        public bool ClickOnce
        {
            get { return ViewState.Get<bool>("ClickOnce", true); }
            set { ViewState["ClickOnce"] = value; }
        }

        [DefaultValue(""), Category("Appearance")]
        public string ClickedText
        {
            get { return ViewState.Get<string>("ClickedText", String.Empty); }
            set { ViewState["ClickedText"] = value; }
        }


        protected override void OnPreRender(EventArgs e)
        {
            string script = String.Format("if( Page_ClientValidate('{0}') ) {{ this.disabled=true; }};", this.ValidationGroup);
            Attributes.Add("onclick", script);

            base.OnPreRender(e);
        }

        internal static string EnsureEndWithSemiColon(string value)
        {
            if (value != null)
            {
                int length = value.Length;
                if ((length > 0) && (value[length - 1] != ';'))
                {
                    return (value + ";");
                }
            }
            return value;
        }

        internal static string MergeScript(string firstScript, string secondScript)
        {
            if (!string.IsNullOrEmpty(firstScript))
            {
                return (firstScript + secondScript);
            }
            if (secondScript.TrimStart(new char[0]).StartsWith("javascript:", StringComparison.Ordinal))
            {
                return secondScript;
            }
            return ("javascript:" + secondScript);
        }


    }
}
